package com.njust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njust.domain.dto.ProjectCreateDTO;
import com.njust.domain.dto.ProjectDTO;
import com.njust.domain.dto.ProjectPageQueryDTO;
import com.njust.domain.po.Equipment;
import com.njust.domain.po.Procedure;
import com.njust.domain.po.Product;
import com.njust.domain.po.Project;
import com.njust.domain.vo.ProjectPageQueryVO;
import com.njust.domain.vo.ProjectVO;
import com.njust.exception.ExcelAreDuplicatedWithDatabase;
import com.njust.exception.ExcelExistsUnknownProduct;
import com.njust.exception.ProjectAlreadyExists;
import com.njust.mapper.*;
import com.njust.service.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/7 13:06
 * @注释
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ComparisonTestMapper comparisonTestMapper;

    @Autowired
    private PersonnelCapabilitieMapper personnelCapabilitieMapper;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private ProcedureService procedureService;

    @Autowired
    private ProcedureMapper procedureMapper;

    @Autowired
    private TestPersonMapper testPersonMapper;

    @Autowired
    private TrainTestPaperMapper trainTestPaperMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public void add(ProjectCreateDTO projectCreateDTO) {
        String name = projectCreateDTO.getProject_name();
        int product_id = projectCreateDTO.getProduct_id();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name", name);
        queryWrapper.eq("product_id", product_id);
        if (projectMapper.selectOne(queryWrapper) != null) {
            throw new ProjectAlreadyExists("项目已存在");
        }
        Project project = new Project(projectCreateDTO);
        projectMapper.insert(project);
    }

    @Override
    public void importExcel(List<ProjectCreateDTO> data, HashSet<String> set) {
        /**
         * 判断Excel表是否存在数据和数据库里的项目重复
         */
        boolean isRepeated = projectMapper.selectList(null).stream().anyMatch(i -> {
            int product_id = i.getProduct_id();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("product_id", product_id);
            return !set.add(productMapper.selectOne(wrapper).getProduct_name() + i.getProject_name());
        });
        if (isRepeated) {
            throw new ExcelAreDuplicatedWithDatabase("Excel表数据和数据库里的项目重复");
        }
        /**
         * 判断Excel表是否存在产品表里没有的产品
         */
        for (ProjectCreateDTO projectCreateDTO : data) {
            String product_name = projectCreateDTO.getProduct_name();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("product_name", product_name);
            if (productMapper.selectOne(wrapper) == null) {
                throw new ExcelExistsUnknownProduct("Excel表中存在未知的产品");
            }
        }
        /**
         * 存入数据库
         */
        for (ProjectCreateDTO projectCreateDTO : data) {
            String product_name = projectCreateDTO.getProduct_name();
            String standard_number = projectCreateDTO.getStandard_number();
            String project_name = projectCreateDTO.getProject_name();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("product_name", product_name);
            int product_id = productMapper.selectOne(queryWrapper).getProduct_id();
            projectMapper.insert(Project.builder()
                    .product_id(product_id)
                    .standard_number(standard_number)
                    .project_name(project_name)
                    .build());
        }
    }
    @Override
    public void update(ProjectDTO projectDTO) {
        int project_id = projectDTO.getProject_id();
        String project_name = projectDTO.getProject_name();
        String standard_number = projectDTO.getStandard_number();

        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("project_id", project_id);

        /**
         * 检测改之后的项目是否已经存在
         */
        int product_id = projectMapper.selectOne(wrapper).getProduct_id();
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("project_name", project_name);
        wrapper1.eq("product_id", product_id);
        if (projectMapper.selectOne(wrapper1) != null) {
            throw new ProjectAlreadyExists("该项目已经存在");
        }


        Project project = Project.builder()
                .project_name(project_name)
                .standard_number(standard_number)
                .build();
        projectMapper.update(project, wrapper);
    }
    @Override
    public ProjectPageQueryVO pageQuery(ProjectPageQueryDTO projectPageQueryDTO){
        Page<Project> page = new Page(projectPageQueryDTO.getPage(), projectPageQueryDTO.getPageSize());
        QueryWrapper wrapper = new QueryWrapper();
        Integer product_id = projectPageQueryDTO.getProduct_id();
        wrapper.eq("product_id", product_id);
        Product product = productMapper.selectOne(wrapper);
        Page<Project> result = projectMapper.selectPage(page, wrapper);

        ProjectPageQueryVO projectPageQueryVO = ProjectPageQueryVO
                .builder()
                .product_id(product_id)
                .description(product.getDescription())
                .total(result.getTotal())
                .records(result.getRecords())
                .build();
        return projectPageQueryVO;
    }

    @Override
    public ProjectVO selectById(int project_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id", project_id);
        Integer product_id = projectMapper.selectOne(queryWrapper).getProduct_id();
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("product_id", product_id);
        String description = productMapper.selectOne(queryWrapper1).getDescription();
        ProjectVO projectVO = ProjectVO
                .builder()
                .project_id(project_id)
                .product_id(product_id)
                .description(description)
                .build();
        return projectVO;
    }
    @Transactional
    public void delete(int project_id){
//      没联删的直接用mapper，有联删再用service
        //删除前 先删除关联的东西
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("project_id",project_id);
        //1.comparison_test
        comparisonTestMapper.delete(wrapper);
        //2.equipment ->equipment_procurement ->solution
        List<Equipment> equipmentList=equipmentMapper.selectList(wrapper);
        for(Equipment e:equipmentList){
            equipmentService.delete(e.getEquipment_id());
        }
        //3.personnel_capabilitie
        personnelCapabilitieMapper.delete(wrapper);
        //4.procedure ->procedure_pdf
        List<Procedure> procedureList=procedureMapper.selectList(wrapper);
        for(Procedure p:procedureList){
            procedureService.delete(p.getProcedure_id());
        }
        //5.test_person
        testPersonMapper.delete(wrapper);

        //6.traning_assessment
        trainTestPaperMapper.delete(wrapper);

        //7.video
        videoMapper.delete(wrapper);

        projectMapper.deleteById(project_id);
    }
}
